import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toastification/toastification.dart';
import 'package:window_manager/window_manager.dart';

import 'bloc/UserBloc.dart';
// 页面布局
import "pages/PadView.dart";
import "pages/PhoneView.dart";
import "pages/WindowsView.dart";

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // 确保 Flutter 绑定已初始化
  final prefs = await SharedPreferences.getInstance();
  final isDarkMode = prefs.getBool('isDarkMode') ?? false;

  // Windows窗口限制
  if(Platform.isWindows){
    await windowManager.ensureInitialized();
    WindowOptions windowOptions = const WindowOptions(
      size: Size(1100, 750),
      minimumSize: Size(1100, 750),
      center: true,
      backgroundColor: Colors.transparent,
      skipTaskbar: false,
      // titleBarStyle: TitleBarStyle.hidden,
    );
    windowManager.waitUntilReadyToShow(windowOptions, () async {
      await windowManager.show();
      await windowManager.focus();
    });
  }
  runApp(
    BlocProvider(
      create: (context) => UserBloc(),
      child: ToastificationWrapper(
        child: MyApp(isDarkMode: isDarkMode),
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  final bool isDarkMode;

  const MyApp({Key? key, required this.isDarkMode}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if(Platform.isAndroid){
      final bool isMobileLayout = MediaQuery.of(context).size.width < 600;
      if(isMobileLayout){
        return PhoneView(isDarkMode: isDarkMode);
      }else{
        return PadView(isDarkMode: isDarkMode);
      }
    }else{   // 目前只支持安卓、Windows
      return WindowsView(isDarkMode: isDarkMode);
    }
  }
}